package com.lczyfz.istep.modules.openstack.api.nova.impl.serveraction;

import com.lczyfz.istep.modules.openstack.api.nova.impl.parseJson.NovaParse;
import com.lczyfz.istep.modules.openstack.api.nova.interfaces.serveraction.ServerSecurityGroupAction;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

public class ServerSecurityGroupActionImpl implements ServerSecurityGroupAction {
	
	public RequestFactory factory = new RequestFactory("nova");
	
	@Override
	public void AddSecurityGroup(String serverid, String securitygroupname) {
			
		JSONObject params = NovaParse.packageAddSecurityGroup(securitygroupname);
	//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/servers/"+serverid + "/action");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		
	}

	@Override
	public void RemoveSecurityGroup(String serverid, String securitygroupname) {
		JSONObject params = NovaParse.packageRemoveSecurityGroup(securitygroupname);
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
				
	}
	
	public static void main(String[] args){

		ServerSecurityGroupActionImpl ServerSecurityGroupdemo = new ServerSecurityGroupActionImpl();
	//	System.out.println("��������Ӱ�ȫ��");
	//	ServerSecurityGroupdemo.AddSecurityGroup("69c30bc5-9d66-4087-b1f2-38b78d9add93", "sg01");
		
	//	System.out.println("������ɾ����ȫ��");
	//	ServerSecurityGroupdemo.RemoveSecurityGroup("69c30bc5-9d66-4087-b1f2-38b78d9add93", "sg01");
		
	}
	
}
